<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" label-width="80px">
      <el-form-item label="订单编号">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流单号">
        <el-input
          v-model="queryParams.logisticsNumber"
          placeholder="请输入物流单号"
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="请选择订单状态"
          size="small"
          style="width: 160px"
        >
          <el-option label="请选择" value="-1" />
          <el-option key="1" label="已发货" value="1" />
          <el-option key="0" label="未发货" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态">
        <el-select
          v-model="queryParams.paymentType"
          placeholder="请选择支付状态"
          size="small"
          style="width: 160px"
        >
          <el-option label="请选择" value="-1" />
          <el-option key="1" label="已支付" value="1" />
          <el-option key="0" label="未支付" value="0" />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" class="create">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddOrder"
          >创建</el-button
        >
        <el-button
          type="danger"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleClearCache"
          >刷新</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="typeList" style="width: 100%">
      <el-table-column type="index" label="序号" align="center" prop="" />
      <el-table-column
        label="订单号"
        align="center"
        prop="orderNumber"
        width="185"
      />

      <el-table-column
        label="客户名称"
        align="center"
        :show-overflow-tooltip="true"
        prop="customer.customerName"
      >
      </el-table-column>
      <el-table-column
        label="开业时间"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
        prop="openTime"
      >
      </el-table-column>
      <el-table-column
        label="安装时间"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
        prop="installTime"
      >
      </el-table-column>
    
      <el-table-column label="订单类型" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.orderType == 1 ? "公司订单" : "工厂订单" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.paymentType == 1 ? "已支付" : "未支付" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.orderStatus == 1 ? "已发货" : "未发货" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="发货截止时间"
        align="center"
        prop="deliverButtonTime"
        width="120"
      >
      </el-table-column>
      <el-table-column
        label="下单人"
        align="center"
        prop="operator"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="下单时间"
        align="center"
        prop="createTime"
        width="120"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="145"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleGoDetail(scope.row, 1)"
            >详情</el-button
          >
          <el-button
            v-if="scope.row.buttonState == 0"
            :class="[scope.row.orderStatus == 1 ? 'nameClass' : '']"
            size="mini"
            type="text"
            @click="handleGoods(scope.row)"
            >发货</el-button
          >
          <el-button
            v-if="scope.row.buttonState == 1"
            size="mini"
            type="text"
            :class="[scope.row.orderStatus == 1 ? 'nameClass' : '']"
            @click="handleApply(scope.row)"
            >发货</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.paymentType == 0"
            @click="handleGoDetail(scope.row, 2)"
            >收款</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.paymentType == 1"
            style="color: #ccc"
            >收款</el-button
          >

          <el-button
            size="mini"
            type="text"
            @click="handleInstall(scope.row)"
            v-if="scope.row.installState == 0"
            >安装</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.installState == 1"
            style="color: #ccc"
            >安装</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 发货弹窗 -->
    <el-dialog title="订单发货" :visible.sync="open" width="500px">
      <el-form
        ref="rules"
        :model="form"
        :rules="rules"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="快递公司" prop="expressCompany">
          <el-input
            v-model="form.expressCompany"
            placeholder="请输入快递公司"
          ></el-input>
        </el-form-item>
        <el-form-item label="快递单号" prop="logisticsNumber">
          <el-input
            v-model="form.logisticsNumber"
            placeholder="请输入快递单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="运费" class="freight" prop="freight">
          <el-input v-model="form.freight" placeholder="请输入运费"></el-input>
          <span>元</span>
        </el-form-item>
        <el-form-item class="dialog-footer" style="margin-bottom: -10px">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="handleShipments('rules')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 申请修改信息弹窗 -->
    <el-dialog title="申请发货" :visible.sync="messageOpen" width="500px">
      <el-form
        ref="editDate"
        :model="editDate"
        :rules="editDateRule"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="申请原因" prop="agencyReason">
          <el-input
            type="textarea"
            resize="none"
            v-model="editDate.agencyReason"
            placeholder="请输入申请原因"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            resize="none"
            v-model="editDate.remarks"
            placeholder="请输入备注信息"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="关闭时间" prop="btnCloseDate">
          <el-date-picker
            v-model="editDate.btnCloseDate"
            style="width: 380px"
            type="datetime"
            placeholder="请选择发货按钮关闭时间"
          >
          </el-date-picker>
        </el-form-item> -->
        <el-form-item class="dialog-footer" style="margin-bottom: -10px">
          <el-button @click="handleCancelMessage">取 消</el-button>
          <el-button type="primary" @click="handleChangeMessage('rules')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  saleList,
  deliverGoods,
  editOrder,
  getOrderInstall,
} from "@/api/sale/index";
export default {
  data() {
    return {
      messageOpen: false, //申请修改订单弹窗
      nowOrderNumber: "", //订单id
      form: {
        orderNumber: "",
        expressCompany: "",
        logisticsNumber: "",
        freight: "",
      },
      rules: {
        //发货正则验证
        expressCompany: [
          { message: "请输入快递名称", trigger: "blur", required: true },
        ],
        logisticsNumber: [
          { message: "请输入快递编号", trigger: "blur", required: true },
        ],
        freight: [{ message: "请输入运费", trigger: "blur", required: true }],
      },
      editDate: {
        remarks: "",
        agencyReason: "",
        agencyApplicant: "admin",
        orderNumber: "",
        agencyState: 0,
        agencyType: 0,
        btnEndTime: "",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      total: 0,
      // 订单列表
      typeList: [],
      // 是否显示弹出层
      open: false,
      // 搜索状态字典
      statusOptions: [], //支付类型
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paymentType: "-1",
        orderStatus: "-1",
        orderNumber: null,
        logisticsNumber: null,
        beginTime: null,
        endTime: null,
      },
      dateRange: [], //日期参数
      editDateRule: {
        agencyReason: [
          {
            required: true,
            message: "请输入申请原因",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.editDate.agencyApplicant = localStorage.getItem("userName");
  },
  methods: {
    handleInstall(row) {
      //安装
      this.$confirm("是否确认安装?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "success",
      }).then(() => {
        getOrderInstall({
          orderId: row.orderId,
          orderNumber: row.orderNumber,
        }).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "安装成功!",
            });
            this.getList();
          } else {
            this.$message({
              type: "warning",
              message: res.msg,
            });
          }
        });
      });
    },
    handleClearCache() {
      window.location.reload();
    },
    handleGoDetail(row, index) {
      //订单详情
      this.$router.push({
        path: "/sale/ordersDetails",
        query: {
          orderId: row.orderId,
          orderNumber: row.orderNumber,
          rowDetail: JSON.stringify(row),
          index: index,
        },
      });
    },
    /** 创建订单*/
    handleAddOrder() {
      // this.sendOpen = true;
      this.$router.push({ path: "/sale/ordersAdd", query: {} });
    },
    handleShipments(formName) {
      //订单发货发货
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.orderNumber = this.nowOrderNumber;
          this.form.freight = Number(this.form.freight).toFixed(2);
          deliverGoods(this.form).then((res) => {
            // console.log(res);
            this.$message({
              message: "已提交发货",
              type: "success",
            });
            this.open = false
            this.getList(); //刷新列表
          });
        } else {
          return false;
        }
      });
    },
    cancel() {
      //发货取消按钮
      this.nowOrderNumber = "";
      this.form = {
        orderNumber: "",
        expressCompany: "",
        logisticsNumber: "",
        freight: "",
      };
      this.editDate.remarks = ""
      this.editDate.agencyReason = ""
      this.open = false;
    },
    /** 查询字典类型列表 */
    getList(index) {
      this.loading = true;
      saleList(this.queryParams).then((response) => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
        // this.dateRange = [];
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // console.log(this.dateRange)
      if (this.dateRange) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }else{
        this.queryParams.beginTime = ""
        this.queryParams.endTime = ""
      }
      this.queryParams.pageNum = 1;
      this.getList();
      this.open = false;
      // setTimeout(() => {
      //   this.queryParams.beginTime = "";
      //   this.queryParams.endTime = "";
      //   this.dateRange = "";
      // }, 200);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        paymentType: "-1",
        orderStatus: "-1",
        orderNumber: "",
        logisticsNumber: "",
        beginTime: "",
        endTime: "",
      };
      this.dateRange = [];
      this.getList();
    },
    handleGoods(row) {
      if (row.orderStatus == 1 || row.buttonState == 1) {
        return false;
      }
      //发货弹窗
      this.nowOrderNumber = row.orderNumber;
      this.open = true;
    },
    handleApply(row) {
      this.editDate.orderNumber = row.orderNumber;
      this.editDate.btnEndTime = row.deliverButtonTime;
      if (row.orderStatus == 1) {
        return false;
      }
      //申请弹窗
      this.nowOrderNumber = row.orderNumber;
      this.messageOpen = true;
    },
    handleCancelMessage() {
      //申请修改取消
      this.nowOrderNumber = "";
      this.editDate = {
        content: "",
        btnCloseDate: "",
      };
      this.messageOpen = false;
    },
    handleChangeMessage() {
      //修改确认
      // console.log(this.editDate)
      editOrder(this.editDate).then((res) => {
        this.$message({
          message: "已提交申请",
          type: "success",
        });
        this.messageOpen = false;
        this.editDate.orderNumber = "";
        this.editDate.btnEndTime = "";
        this.open = false
        this.getList();
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.nameClass {
  color: #ccc !important;
}
.el-range-editor {
  width: 100% !important;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.app-container {
  .el-col-11 {
    width: 100%;
  }
  .el-form-item__content {
    position: relative !important;
    span {
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
      position: absolute;
      top: 1px;
      right: 12px;
    }
  }
  .mb8 {
    .title {
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
    }
  }
}
</style>